//
//  BodyFileViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/12/16.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "BodyFileViewController.h"
#import "BodayFileViewMode.h"
#import "FFNetworkHelper.h"

#define kBodayFileHeadCell @"BodayFileHeadCell"
#define kBodayFileInfoCell @"BodayFileInfoCell"

@interface BodyFileViewController () {
    NSMutableArray *_sections;
}

@property (nonatomic, strong) NSMutableArray *dataSourceMAttay;
@property (nonatomic, strong) NSMutableArray *dataInfoArray;
@end

@implementation BodyFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"身体档案";
    [self registCell];
    self.data = @[].mutableCopy;
    _dataSourceMAttay = @[@"身高",@"胸围",@"腰围",@"臀围",@"体脂率",@"内脏脂肪",@"肌肉含量",@"身高",@"BMI"].mutableCopy;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _dataInfoArray = @[].mutableCopy;
    [self getBodayFileInfo];
}

#pragma -mark ------------------网络请求方法----------------------

- (void)getBodayFileInfo {
    [FFNetworkHelper POST:@"bodyData/getLastData" parameters:@{} success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            [self.data addObject:[BodyFileMode yy_modelWithJSON:responseObject[@"data"][@"bodyData"]]];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.tableView.canReload = YES;
            [self.tableView reloadData];
        });
    } failure:^(NSError *error) {
        
    }];
}

#pragma -mark ------------------cell的注册方法----------------------

- (void)registCell {
    [self.tableView registerClass:[BodayFileHeadCell class] forCellReuseIdentifier:kBodayFileHeadCell];
    [self.tableView registerClass:[BodayFileInfoCell class] forCellReuseIdentifier:kBodayFileInfoCell];
}

#pragma -mark ------------------tableView的代理方法----------------------

- (NSArray *)customSections {
    if (!_sections) {
        _sections = [NSMutableArray array];
        MCUTableCustomSection *sec = [MCUTableCustomSection createWithTarget:self];
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.cellForRowAtIndexSelector = @selector(cellViewForBodyHeadCell:);
        sec.numberOfRowsSelector = @selector(cellNumberForBodyHeadCell:);
        sec.heightForCellSelector = @selector(heightCellForBodyHeadCell:);
        sec.cellSelectedSelector = @selector(selectedForBodyHeadCell:);
        [_sections addObject:sec];
        
        //中间的cell
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.cellForRowAtIndexSelector = @selector(cellViewForBodyInfodCell:);
        sec.heightForCellSelector = @selector(heightCellForBodyInfodCell:);
        sec.numberOfRowsSelector = @selector(cellNumberForBodyInfodCell:);
        sec.cellSelectedSelector = @selector(selectCellForBodyInfodCell:);
        [_sections addObject:self.splitSection];
        [_sections addObject:sec];
    }
    return _sections;
}

#pragma -mark ------------------cell的代理方法----------------------

- (NSNumber *)heightCellForBodyHeadCell:(NSIndexPath *)indexPath {
    return  @50;
}
- (NSNumber *)cellNumberForBodyHeadCell:(NSIndexPath *)indexPath{
    return @1;
}

- (void)selectedForBodyHeadCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)cellViewForBodyHeadCell:(NSIndexPath *)indexPath {
    BodayFileHeadCell *cell = [[BodayFileHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBodayFileHeadCell];
    return cell;
}

- (NSNumber *)heightCellForBodyInfodCell:(NSIndexPath *)indexPath {
    return  @80;
}
- (NSNumber *)cellNumberForBodyInfodCell:(NSIndexPath *)indexPath{
    return @9;
}

- (void)selectCellForBodyInfodCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)cellViewForBodyInfodCell:(NSIndexPath *)indexPath {
    BodayFileInfoCell *cell = [[BodayFileInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBodayFileInfoCell];
    cell.leftLabel.text =_dataSourceMAttay[indexPath.row];
    cell.rightLabel.text = [self creatInfoArray][indexPath.row];
    return cell;
}

- (NSMutableArray *)creatInfoArray{
    BodyFileMode *mode = self.data[0];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ldcm",mode.height]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ldKG",mode.weight]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ldcm",mode.bust]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ldcm",mode.waist]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ldcm",mode.hip]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ld%%",mode.body_fat]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ld%%",mode.visceral_fat]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ld%%",mode.muscle_content]];
    [_dataInfoArray addObject:[NSString stringWithFormat:@"%ld",mode.bmi]];
    return _dataInfoArray;
}
@end
